import SimObject from "../../../../src/util/sim/SimObject";
import THREE, { SphereGeometry, ImageUtils, Texture, Mesh, MeshPhongMaterial, Object3D, ShaderLib, UniformsUtils, ShaderMaterial, SphereBufferGeometry, BufferGeometryUtils } from "three";

export default class Earth extends SimObject {

    public static ROTATION_Y: number = 0.0025;
    public static TILT: number = 0.41;
    public static CLOUDS_SCALE: number = 1.005;
    public static CLOUDS_ROTATION_Y: number = Earth.ROTATION_Y * 0.95;

    // public earthmap: string;
    // public geometry: SphereGeometry;
    // public texture: Texture;
    // public material: MeshPhongMaterial;
    public globeMesh!: Mesh;
    public cloudsMesh!: Mesh;

    public constructor() {
        super();

        let earthGroup: Object3D = new Object3D();
        this.setObject3D(earthGroup);

        // this.texture = ImageUtils.loadTexture(this.earthmap);
        // this.material = new MeshPhongMaterial({ map: this.texture });
        // this.mesh = new Mesh(this.geometry, this.material);

        // this.mesh.rotation.x = Earth.TILT;
        // this.setObject3D(this.mesh);
        this.createGlobe();
        this.createClouds();
    }

    public update(): void {
        // this.globeMesh.rotation.y += Earth.ROTATION_Y;
        // this.cloudsMesh.rotation.y += Earth.CLOUDS_ROTATION_Y;
        // super.update();
    }

    private createGlobe(): void {
        const surfaceMap: Texture = ImageUtils.loadTexture('../../../images/earth_surface_2048.jpg');
        const normalMap: Texture = ImageUtils.loadTexture('../../../images/earth_normal_2048.jpg');
        const specularMap: Texture = ImageUtils.loadTexture('../../../images/earth_specular_2048.jpg');

        let shader: any = ShaderLib["normal"];
        let uniforms: any = UniformsUtils.clone(shader.uniforms);
        // uniforms["tNormal"].texture = normalMap;
        // uniforms["tDiffuse"].texture = surfaceMap;
        // uniforms["tSpecular"].texture = specularMap;

        // uniforms["enableDiffuse"].value = true;
        // uniforms["enableSpecular"].value = true;

        let shaderMaterial: ShaderMaterial = new ShaderMaterial({
            fragmentShader: shader.fragmentShader,
            vertexShader: shader.vertexShader,
            uniforms: uniforms,
            lights: true
        });

        let globeGeometry: SphereBufferGeometry = new SphereBufferGeometry(1, 32, 32);
        let a = THREE.BufferGeometryUtils.computeTangents(globeGeometry);

        this.globeMesh = new Mesh(globeGeometry, shaderMaterial);
        this.globeMesh.rotation.x = Earth.TILT;

        this.object3D.add(this.globeMesh);

    }

    private createClouds(): void {

    }
}